package com.codefish.framework.spring.core.io;

import com.codefish.framework.spring.core.utils.AssertUtils;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

/**
 * @author <a href='https://gitee.com/hzf2281'>codefish</a>
 * @version 1.0.0
 * @since 2022/09/30 下午 03:15
 */
public class FileSystemResource implements Resource {
    private final String path;
    private final File file;

    public FileSystemResource(@NotNull String path) {
        AssertUtils.notNull(path, "文件路径不能为空");
        this.path = path;
        this.file = new File(path);
    }

    @Override
    @Nullable
    public InputStream getInputStream() throws IOException {
        InputStream is = null;
        try {
            is = Files.newInputStream(file.toPath());
        } catch (FileNotFoundException fne) {
            fne.printStackTrace();
        }
        return is;
    }

    @Override
    public String getResourceName() {
        return this.file.getName();
    }

    @Override
    public String getResourcePath() {
        return this.file.getAbsolutePath();
    }
}
